import {
    http,
    defaultSuccess,
    defaultError
}
    from "@/plugins/axios";

export default {
    /**
     * 发送验证码
     * @param {String} phone 手机号
     * @param {Function} success 成功回调
     * @param {Function} error 失败回调
     */
    captcha(phone, success = defaultSuccess, error = defaultError) {
        return http.get("/sms/send/captcha", {
            params: {
                phone: phone
            }
        }).then(success).catch(error)
    },
    /**
     *
     * @param {Object} user 登录用户信息
     * @param {Function} success 成功回调
     * @param {Function} error 失败回调
     */
    login(user, success = defaultSuccess, error = defaultError) {
        http.post("/user/login", user).then(success).catch(error)
    },
    /**
     * 检查token是否有效
     * @param {Function} success 成功回调
     * @param {Function} error 失败回调
     */
    check(success = defaultSuccess, error = defaultError) {
        http.get("/test").then(success).catch(error)
    },
    /**
     * 用户列表
     * @param page
     * @param success
     * @param error
     */
    list(page, success = defaultSuccess, error = defaultError) {
        http.get("/user/list", {params: {page: page}}).then(success).catch(error)
    },
    /**
     * 用户搜索
     * @param key
     * @param page
     * @param success
     * @param error
     */
    search(key, page, success = defaultSuccess, error = defaultError) {
        http.get("/user/search", {params: {key: key, page: page}}).then(success).catch(error)
    },
    /**
     * 查询指定用户的权限
     * @param id
     * @param success
     * @param error
     */
    getUserPermissions(id, success = defaultSuccess, error = defaultError) {
        http.get("/user/permission/" + id).then(success).catch(error)
    },
    /**
     * 设置权限
     * @param param
     * @param success
     * @param error
     */
    grantPermission(param, success = defaultSuccess, error = defaultError) {
        http.post("/user/grant/permission", param).then(success).catch(error);
    },
    /**
     * 设置角色
     * @param param
     * @param success
     * @param error
     */
    grantRole(param, success = defaultSuccess, error = defaultError) {
        http.post("/user/grant/role", param).then(success).catch(error);
    },
    /**
     * 查询指定用户角色
     * @param id
     * @param success
     * @param error
     */
    getUserRoles(id, success = defaultSuccess, error = defaultError) {
        http.get("/user/role/" + id).then(success).catch(error)
    },
    /**
     * 详情
     * @param id
     * @param success
     * @param error
     */
    detail(id, success = defaultSuccess, error = defaultError) {
        http.get("/user/" + id).then(success).catch(error)
    },
    /**
     * 添加用户
     * @param param
     * @param success
     * @param error
     */
    save(param, success = defaultSuccess, error = defaultError) {
        http.post("/user/add", param).then(success).catch(error)
    },
    /**
     * 更新用户信息
     * @param param
     * @param success
     * @param error
     */
    update(param, success = defaultSuccess, error = defaultError) {
        http.post("/user/edit", param).then(success).catch(error)
    },
    /**
     * 根据角色查询用户
     * @param roleId
     * @param success
     * @param error
     */
    getReceiveTaskUsers(roleGroup, templateId, success = defaultSuccess, error = defaultError) {
        http.get("/user/list/" + roleGroup + "/" + templateId).then(success).catch(error)
    }
}